package com.cdtu.store.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cdtu.store.common.entity.BrandEntity;
import com.cdtu.store.common.entity.CategoryBrandEntity;
import com.cdtu.store.common.entity.CategoryEntity;
import com.cdtu.store.item.dao.BrandDao;
import com.cdtu.store.item.dao.CategoryDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.store.common.utils.PageUtils;
import com.cdtu.store.common.utils.Query;

import com.cdtu.store.item.dao.CategoryBrandDao;
import com.cdtu.store.item.service.CategoryBrandService;


@Service("categoryBrandService")
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandDao, CategoryBrandEntity> implements CategoryBrandService {
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandEntity> page = this.page(
                new Query<CategoryBrandEntity>().getPage(params),
                new QueryWrapper<CategoryBrandEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void removeCategoryBrand(CategoryBrandEntity categoryBrand) {
        this.remove(
                new QueryWrapper<CategoryBrandEntity>().
                        eq("brand_id",categoryBrand.getBrandId()).
                        eq("category_id",categoryBrand.getCategoryId()));
    }

    @Override
    public void updateBrand(Long id, String name) {
        CategoryBrandEntity categoryBrandEntity = new CategoryBrandEntity();
        categoryBrandEntity.setBrandId(id);
        categoryBrandEntity.setBrandName(name);
        this.update(categoryBrandEntity,new UpdateWrapper<CategoryBrandEntity>().eq("brand_id",id));
    }

    @Override
    public void updateCategory(Long categoryId, String name) {
        CategoryBrandEntity categoryBrandEntity = new CategoryBrandEntity();
        categoryBrandEntity.setCategoryId(categoryId);
        categoryBrandEntity.setCategoryName(name);
        this.update(categoryBrandEntity,new UpdateWrapper<CategoryBrandEntity>().eq("category_id",categoryId));
    }

    @Override
    public void saveDetail(CategoryBrandEntity categoryBrand) {
        Long brandId = categoryBrand.getBrandId();
        Long categoryId = categoryBrand.getCategoryId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(categoryId);
        categoryBrand.setBrandName(brandEntity.getName());
        categoryBrand.setCategoryName(categoryEntity.getName());
        this.save(categoryBrand);
    }
}